package com.eryos.android.cigarettecounter;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.eryos.android.cigarettecounter.beans.History;
import com.eryos.android.cigarettecounter.database.CigaretteDatabase;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public abstract class RootActivity extends Activity  {

	public static final String PREFS_NAME = "eryos-config";
	public static final String SDCARD_NAME = "cigarettecounter.xml";

	private ProgressDialog pd; 

	protected  GoogleAnalyticsTracker tracker;

	public enum HISTO{WEEKLY, MONTHLY};

	protected CigaretteDatabase db;
	
	static {
		
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new CigaretteDatabase(this);        
        db.open();
    
        
        // Google Analytics Tracker
        tracker = GoogleAnalyticsTracker.getInstance();
        tracker.start("UA-8541799-7", this);
        
    	tracker.trackEvent(
                "OS",  									// Category
                "Android",  							// Action
                "Version", 								// Label
                android.os.Build.VERSION.SDK_INT);      // Value
        
        // Ads
//        AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
        
        // Initialize the UI
        initUI();
        
        // Start & Initialize DB 
        initValues();
    }

    protected abstract void initUI();

    protected abstract void initValues();
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.weekly:
        	launchWeeklyStats();
            return true;
        case R.id.monthly:
        	launchMonthlyStats();
            return true;
        case R.id.home:
        	launchHome();
            return true;
        case R.id.param:
        	launchConfig();
            return true;
        default:
        	return false;
        }
    }
    
    
    @Override
    public void onRestart() {
    	super.onRestart();
    	db.open();
    	initValues();    	
    }
    

    public void launchMonthlyStats(){  
    	tracker.trackPageView("MonthlyStats");
        tracker.dispatch();
    	db.close();
    	Intent intent = new Intent(RootActivity.this, HistoryActivity.class);
    	intent.putExtra(History.class.toString(), new History(History.Type.MONTHLY));
    	startActivity(intent);
    	if ( ! (this instanceof CigaretteCounter) )
    		finish();
    }
    
    public void launchWeeklyStats(){
    	tracker.trackPageView("WeeklyStats");
        tracker.dispatch();
    	db.close();
    	Intent intent = new Intent(RootActivity.this, HistoryActivity.class);
    	intent.putExtra(History.class.toString(), new History(History.Type.WEEKLY));
    	startActivity(intent);
    	if ( ! (this instanceof CigaretteCounter) )
    		finish();
    }
    
    public void exitApplication(){
    	db.close();
    	finish();
    }
    
    public void launchHome(){
    	tracker.trackPageView("Home");
        tracker.dispatch();
    	db.close();
    	Intent intent = new Intent(RootActivity.this, CigaretteCounter.class);
		startActivity(intent);
		finish();
    }
    
    public void launchConfig(){
    	tracker.trackPageView("Config");
        tracker.dispatch();
    	db.close();
    	Intent intent = new Intent(RootActivity.this, ConfigActivity.class);
    	startActivity(intent);   
    	if ( ! (this instanceof CigaretteCounter) )
    		finish();
    }
    
    @Override
    protected void onDestroy() {
      super.onDestroy();
      db.close();
      tracker.dispatch();
      tracker.stop();
    }	


    /**
     * Show a waiting popup
     */
    public void showWaitingMessage(){
    	pd = ProgressDialog.show(RootActivity.this, "",  "Loading. Please wait...", true);
    	//pd.show();
    }
    
    /**
     * hide the waiting popup
     */
    public void hideWaitingMessage(){
    	System.out.println("hideWaitingMessage");
    	if ( pd != null ) {
    		System.out.println("dismiss");
    		pd.dismiss();
    		pd.cancel();
    	}
    }

    public void hideWaitingMessage(int ID){
    	hideWaitingMessage();
    	Toast.makeText(this, ID, Toast.LENGTH_SHORT).show();
    }
}
